/**
 * 发送get请求
 * @param {string} url 
 * @param {string} param 
 * @param {Function} callback 
 * @param {Object} headerFn 
 */

function get(url,param,callback,headerFn={},dataType = 'json'){
    const xhr = new XMLHttpRequest
    xhr.onreadystatechange = () =>{
        if(xhr.readyState === 4){
            if(xhr.status === 200){
                let res =xhr.responseText
                switch (dataType) {
                    case 'json':
                        res = JSON.parse(res)
                        break;
                    default:
                        break;
                }
                callback(res)
            }else{
                console.log('请求有误：', xhr.status);
            }
        }
    }
}

xhr.open('get',`${url}?${param}`)
for (const key in header) {
        xhr.setRequestHeader(key,header[key])
    }
xhr.send(null)

/**
 * 发送post请求
 * @param {string} url 
 * @param {string} param 
 * @param {Function} callback 
 * @param {Object} headerFn 
 */

function post(url,param,callback,headerFn={},dataType = 'json'){
    const xhr = new XMLHttpRequest
    xhr.onreadystatechange = () =>{
        if(xhr.readyState === 4){
            if(xhr.status === 200){
                let res =xhr.responseText
                switch (dataType) {
                    case 'json':
                        res = JSON.parse(res)
                        break;
                    default:
                        break;
                }
                callback(res)
            }else{
                console.log('请求有误：', xhr.status);
            }
        }
    }
}

xhr.open('post',url)
xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
for (const key in headers) {
        xhr.setRequestHeader(key,header[key])
    }
xhr.send(params)